package com.jcfk.eam.service.mould;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.mould.EamMouldEmailRecordFileDao;
import com.jcfk.eam.domain.dto.mould.EamMouldEmailRecordFileDTO;
import com.jcfk.eam.domain.po.mould.EamMouldEmailRecordFile;
import com.jcfk.eam.domain.query.mould.EamMouldEmailRecordFileQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 模具报警记录工单文件
 *
 * @author ly
 * @email 522891237@gqq.com
 * @date 2025-08-13
 */
@Service
public class EamMouldEmailRecordFileService extends BaseService<EamMouldEmailRecordFileDao, EamMouldEmailRecordFile> {

    /**
     * EamMouldEmailRecordFile分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamMouldEmailRecordFileDTO> queryByPage(DataPage<EamMouldEmailRecordFileDTO> page, EamMouldEmailRecordFileQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamMouldEmailRecordFile是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamMouldEmailRecordFileDTO dto) {
        QueryWrapper<EamMouldEmailRecordFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        return RetResult.ok();
    }

    /**
     * 插入模具报警记录工单文件
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamMouldEmailRecordFileDTO dto) {

        // 保存
        EamMouldEmailRecordFile item = new EamMouldEmailRecordFile();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新模具报警记录工单文件
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamMouldEmailRecordFileDTO dto) {

        // 保存
        EamMouldEmailRecordFile item = new EamMouldEmailRecordFile();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除模具报警记录工单文件(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamMouldEmailRecordFileDTO dto) {

        EamMouldEmailRecordFile item = new EamMouldEmailRecordFile();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamMouldEmailRecordFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamMouldEmailRecordFile item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取模具报警记录工单文件信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamMouldEmailRecordFileDTO get(String id) {
        // 查询数据
        EamMouldEmailRecordFile item = this.baseMapper.selectById(id);
        EamMouldEmailRecordFileDTO dto = new EamMouldEmailRecordFileDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamMouldEmailRecordFileDTO>
     */
    public List<EamMouldEmailRecordFileDTO> getList(EamMouldEmailRecordFileQuery query) {
        // 查询条件
        QueryWrapper<EamMouldEmailRecordFile> qw = new QueryWrapper<>();
        qw.lambda().eq(EamMouldEmailRecordFile::getDeleted, 0);
        qw.lambda().eq(!StrUtil.isEmpty(query.getEmailRecordId()), EamMouldEmailRecordFile::getEmailRecordId, query.getEmailRecordId());
        // 查询数据
        List<EamMouldEmailRecordFile> list = super.list(qw);

        return convertList(list, EamMouldEmailRecordFileDTO.class);
    }

    /**
     * 根据主键获取模具报警记录工单文件信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        EamMouldEmailRecordFile item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }


    /**
     * 保存删除文件
     *
     * @param files
     * @param emailRecordId
     */
    public void saveFileBatch(List<EamMouldEmailRecordFileDTO> files, String emailRecordId) {
        QueryWrapper<EamMouldEmailRecordFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamMouldEmailRecordFile::getDeleted, 0);
        queryWrapper.lambda().eq(EamMouldEmailRecordFile::getEmailRecordId, emailRecordId);
        List<EamMouldEmailRecordFile> eamMouldEmailRecordFileList = this.list(queryWrapper);
        List<EamMouldEmailRecordFile> insertList = files.stream().filter(item -> StrUtil.isEmpty(item.getId()))
                .map(item -> {
                    EamMouldEmailRecordFile dto = new EamMouldEmailRecordFile();
                    BeanUtils.copyProperties(item, dto);
                    dto.setEmailRecordId(emailRecordId);
                    this.setDefaultValue(dto);
                    return dto;
                }).collect(Collectors.toList());
        List<EamMouldEmailRecordFile> delList = eamMouldEmailRecordFileList.stream().filter(item ->
                files.stream().noneMatch(item1 ->
                        StrUtil.isNotEmpty(item1.getId()) && item1.getId().equals(item.getId()))).peek(item -> {
            item.setDeleted(1);
        }).collect(Collectors.toList());
        if (!insertList.isEmpty()) {
            this.saveBatch(insertList);
        }
        if (!delList.isEmpty()) {
            this.updateBatchById(delList);
        }
    }
}

